package grsu.epam.airline.action;

import java.util.HashMap;
import java.util.Map;

import grsu.epam.airline.form.CrewForm;
import grsu.epam.airline.service.FlightCrewService;
import grsu.epam.airline.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.LookupDispatchAction;

public class FlightCrewAction extends LookupDispatchAction {
	String target = ForwardActionTo.FLIGHTCREW;

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			target = ForwardActionTo.ADDFLIGHTCREW;
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			CrewForm input = (CrewForm) form;
			FlightCrewService flightCrewService = new FlightCrewService();
			if (input.getFlightCrewId() != 0) {
				flightCrewService.deleteFlightCrew(input.getFlightCrewId());
				target = ForwardActionTo.FLIGHTCREW;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("chooseFlightCrew", new ActionMessage(
						"error.flightcrew"));
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
			}
			request.setAttribute("flightCrews",
					flightCrewService.getAllFlightCrew());
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			CrewForm input = (CrewForm) form;
			FlightCrewService flightCrewService = new FlightCrewService();
			if (input.getFlightCrewId() != 0) {
				request.setAttribute("flightCrew", flightCrewService
						.getFlightCrewById(input.getFlightCrewId()));
				target = ForwardActionTo.UPDATEFLIGHTCREW;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("chooseFlightCrew", new ActionMessage(
						"error.flightcrew"));
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
				request.setAttribute("flightCrews",
						flightCrewService.getAllFlightCrew());
			}
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);

	}

	@Override
	protected Map getKeyMethodMap() {
		Map m = new HashMap();
		m.put("flightCrew.delete", "delete");
		m.put("flightCrew.modify", "modify");
		m.put("flightCrew.add", "add");

		return m;
	}

}